import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;

public class Choice implements ActionListener {
    // 创建组件
    static JLabel userName = new JLabel();
    static JButton btn01 = new JButton("匹配对局");
    static JButton btn02 = new JButton("查询战绩");
    static JButton btn03 = new JButton("退出");
    static JFrame jf = new JFrame("德州扑克 - 选择");
    public static String n;

    public static void main(String[] args) {
        new Choice(null);
    }

    public Choice(String name) {
        n = name;
        userName.setText("欢迎你! " + name);
        Font f1 = new Font("微软雅黑", Font.PLAIN, 17);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(285, 190);
        jf.setResizable(false);

        btn01.addActionListener(this);
        btn02.addActionListener(this);
        btn03.addActionListener(this);

        // 创建内容面板容器
        JPanel panel = new JPanel();
        panel.add(userName);
        userName.setFont(f1);
        btn01.setFont(f1);
        btn02.setFont(f1);
        btn03.setFont(f1);
        panel.add(btn01);
        panel.add(btn02);
        panel.add(btn03);
        FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
        panel.setLayout(layout);
        layout.setHgap(100);
        jf.setContentPane(panel);
        jf.setLocationRelativeTo(null);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn01) {
            Test.link.println("<Play>");
            btn01.setEnabled(false);
            btn02.setEnabled(false);
            btn03.setEnabled(false);
            new Matching().start();
        } else if (e.getSource() == btn02) {
            Test.link.println("<Query>");
            try {
                if (Test.link.readLine().equals("OK")) {
                    new QueryTheRecord(jf);
                    jf.setVisible(false);
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } else if (e.getSource() == btn03) {
            Test.link.println("<Bye>");
            System.exit(0);
        }
    }

    static class Matching extends Thread {
        @Override
        public void run() {
            for (int i = 1; i <= 40; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                btn01.setText("匹配中...(" + i + "/40)");
                try {
                    Test.link.println("<number?>");
                    int number = Integer.parseInt(Test.link.readLine());
                    System.out.println(i + ": 准备人数: (" + number + "/4)");
                    if (number == 4) {
                        System.out.println("匹配完成");
                        Test.link.println("<go>");
                        if (Test.link.readLine().equals("OK")) {
                            jf.setVisible(false);
                            new Match();
                        }
                        break;
                    }
                } catch (IOException ioe) {
                    System.err.println("服务器无法连接");
                } catch (NumberFormatException nfe) {
                    System.err.println("接收到异常数据");
                }
                if (i == 40) {
                    Test.link.println("<overtime>");
                }
            }
            btn01.setEnabled(true);
            btn02.setEnabled(true);
            btn03.setEnabled(true);
            btn01.setText("重新匹配");
        }
    }
}
